package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DictionaryItem;
import cn.wolfcode.crm.query.DictionaryItemQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionaryItemService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dictionaryItem")
public class DictionaryItemController {

    @Autowired
    IDictionaryItemService dictionaryItemService;

    @RequestMapping("")
    @RequiresPermissions(value = {"字典明细首页","dictionaryItem:index"} ,logical = Logical.OR)
    public String index(){

        return "dictionaryItem/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"字典明细列表","dictionaryItem:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(QueryObject qo){
        PageResult result = dictionaryItemService.query(qo);
        return result;
    }
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(QueryObject qo){

        return dictionaryItemService.list();
    }
    @RequestMapping("getDictionaryItemByParentId")
    @ResponseBody
    public Object getDictionaryItemByParentId(Long id){
        return dictionaryItemService.getDictionaryItemByParentId(id);
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"字典明细保存","dictionaryItem:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(DictionaryItem entity){
        JSONResult result = new JSONResult();
        try{
            dictionaryItemService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"字典明细更新","dictionaryItem:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(DictionaryItem entity){
        JSONResult result = new JSONResult();
        try{
            dictionaryItemService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"字典明细删除","dictionaryItem:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            dictionaryItemService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
    @RequestMapping("getDictionaryItemByParentName")
    @RequiresPermissions(value = {"字典明细查询","dictionaryItem:getDictionaryItemByParentName"} ,logical = Logical.OR)
    @ResponseBody
    public List<Map<Object,Object>> getDictionaryItemByParentName(DictionaryItemQueryObject dio){
        return dictionaryItemService.getDictionaryItemByParentName(dio);
    }

}
